// $Id$
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Xml.Serialization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataRules;
#if (!NET_CF)
using System.Diagnostics.CodeAnalysis;
#endif

namespace Northwind.DomainModel
{

    /// <summary />
    partial interface IOrderDetailsContainer
    {
        /// <summary />
      OrderDetails OrderDetails
        {
            get;
            set;
        }

    }

    /// <summary />
    partial interface IOrderDetailsMapper
    {

        /// <summary />
        OrderDetails Find(OrderDetails.PrimaryKey key, DataMapperCollection dataMappers);
        /// <summary />
        DomainModelKeyedCollection<OrderDetails> FindAll(DataMapperCollection dataMappers);

    }

    /// <summary />
    partial class OrderDetails
    {

        /// <summary />
        partial class PrimaryKey
        {
            /// <summary />
            public struct Key
            {
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public int OrderID;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public int ProductID;
          
                /// <summary />
                public Key(int orderid,int productid)
                {
                OrderID=orderid;
                ProductID=productid;
                }

                /// <summary />
                public override string ToString()
                {
                    return string.Format(
                        CultureInfo.CurrentCulture,
                        "{0}|{1}",
                        OrderID,
                        ProductID
                    );
                }
            }

            /// <summary />
            public static implicit operator PrimaryKey(PrimaryKey.Key key)
            {
                return new PrimaryKey(key);
            }

            /// <summary />
            public static explicit operator PrimaryKey.Key(PrimaryKey key)
            {
                return key.Value;
            }

        }

        /// <summary />
        partial class DataTransfer
        {
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public int OrderID;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public int ProductID;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public decimal? UnitPrice;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public short? Quantity;
            /// <summary />
#if (!NET_CF)
            [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
#endif
            public float? Discount;

            /// <summary />
            public object Clone()
            {
                return MemberwiseClone();
            }

        }

        /// <summary />
        protected override OrderDetails.PrimaryKey CreateId()
        {
            return new OrderDetails.PrimaryKey(new OrderDetails.PrimaryKey.Key(Data.OrderID, Data.ProductID));
        }

        /// <summary />
        internal OrderDetails.PrimaryKey GetId()
        {
            return Id;
        }

        /// <summary />
        internal void SetId(PrimaryKey.Key id)
        {
            Data.OrderID=id.OrderID;
            Data.ProductID=id.ProductID;
        }


        /// <summary />
#if (!NET_CF)
        [DataObjectMethod(DataObjectMethodType.Select, false)]
#endif
        public static OrderDetails Find(OrderDetails.PrimaryKey key, DataMapperCollection dataMappers)
        {
            return ((IOrderDetailsMapper)dataMappers[typeof(OrderDetails)]).Find(key, dataMappers);
        }

        /// <summary />
#if (!NET_CF)
        [DataObjectMethod(DataObjectMethodType.Select, true)]
#endif
        public static IList<OrderDetails> FindAll(DataMapperCollection dataMappers)
        {
            return ((IOrderDetailsMapper)dataMappers[typeof(OrderDetails)]).FindAll(dataMappers);
        }

        /// <summary />
        private void _LoadProduct(bool lazyload)
        {
            if (IsNew)
                return;

            if (_Product==null)
                _Product=((IProductMapper)DataMappers[typeof(Product)]).Find(Data.OrderID, DataMappers);
        }

        /// <summary />
        private void _Product_Saved(object sender, PersistenceEventArgs e)
        {
            int id=((Product)sender).GetId().Value;
            if (Data.OrderID!=id)
            {
                Data.OrderID=id;
                IsModified=true;
            }

            ((Product)sender).Saved-=_Product_Saved;
        }

        /// <summary />
        private void _LoadOrder(bool lazyload)
        {
            if (IsNew)
                return;

            if (_Order==null)
                _Order=((IOrderMapper)DataMappers[typeof(Order)]).Find(Data.ProductID, DataMappers);
        }

        /// <summary />
        private void _Order_Saved(object sender, PersistenceEventArgs e)
        {
            int id=((Order)sender).GetId().Value;
            if (Data.ProductID!=id)
            {
                Data.ProductID=id;
                IsModified=true;
            }

            ((Order)sender).Saved-=_Order_Saved;
        }

        /// <summary />
        internal bool InternalIsNew
        {
            get
            {
                return IsNew;
            }
        }

        /// <summary />
        private Product _ProductProperty
        {
            get
            {
                Load(true);
                return _Product;
            }
            set
            {
                _LoadProduct(true);
                if (_Product!=value)
                {
                    if ((_Product!=null) && (_Product.InternalIsNew))
                        _Product.Saved-=_Product_Saved;
                    _Product=value;
                    if (value!=null)
                    {
                        if (_Product.InternalIsNew)
                            _Product.Saved+=_Product_Saved;
                        else
                            Data.OrderID=(int)value.GetId();
                    }
                }
            }
        }

        /// <summary />
        private Order _OrderProperty
        {
            get
            {
                Load(true);
                return _Order;
            }
            set
            {
                _LoadOrder(true);
                if (_Order!=value)
                {
                    if ((_Order!=null) && (_Order.InternalIsNew))
                        _Order.Saved-=_Order_Saved;
                    _Order=value;
                    if (value!=null)
                    {
                        if (_Order.InternalIsNew)
                            _Order.Saved+=_Order_Saved;
                        else
                            Data.ProductID=(int)value.GetId();
                    }
                }
            }
        }

        /// <summary />
        private decimal? _UnitPriceProperty
        {
            get
            {
                Load(true);
                return Data.UnitPrice;
            }
            set
            {
                if (Data.UnitPrice!=value)
                {
                    Data.UnitPrice=value;
                }
            }
        }

        /// <summary />
        private short? _QuantityProperty
        {
            get
            {
                Load(true);
                return Data.Quantity;
            }
            set
            {
                if (Data.Quantity!=value)
                {
                    Data.Quantity=value;
                }
            }
        }

        /// <summary />
        private float? _DiscountProperty
        {
            get
            {
                Load(true);
                return Data.Discount;
            }
            set
            {
                if (Data.Discount!=value)
                {
                    Data.Discount=value;
                }
            }
        }

        /// <summary />
        private Product _Product;
        /// <summary />
        private Order _Order;

        /// <summary />
        public static readonly IdentityMap<OrderDetails.PrimaryKey, OrderDetails> IdentityMap=new IdentityMap<OrderDetails.PrimaryKey, OrderDetails>();

    }

}
